<?php

namespace App\Models;

use App\Models\Observer\PlatUserObserver;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Tymon\JWTAuth\Contracts\JWTSubject;

use Illuminate\Foundation\Auth\User as Authenticatable;

class PlatUser extends  Authenticatable implements
    Transformable,
    JWTSubject
{
    use TransformableTrait;

    protected $table = 'plat_users';
//    protected $visible = ['id', 'role', 'username', 'status', 'last_at', 'last_ip', 'is_profiled','is_remitted'];
    protected $appends = ['is_profiled', 'is_remitted'];
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        PlatUser::observe(PlatUserObserver::class);
    }

    //relations
    public function apps()
    {
        return $this->hasMany(PlatUserApp::class, 'uid');
    }

    public function upper()
    {
        return $this->belongsTo(PlatUser::class, 'upper_id');
    }

    public function profile()
    {
        return $this->hasOne(PlatUserProfile::class, 'uid');
    }

    public function assets()
    {
        return $this->hasOne(AssetCount::class, 'uid');
    }

    public function bank()
    {
        return $this->hasMany(PlatUserBank::class, 'uid');
    }

    //scope
    public function scopeProxy($query)
    {
        return $query->where('role', 1);
    }

    public function scopeAudited($query)
    {
        return $query->where('status', 1);
    }

    public function scopeNormal($query)
    {
        return $query->where('status', 1);
    }

    public function scopeBycode($query, $code)
    {
        return $query->where('code', $code);
    }

    public function scopeCanRecharge($query)
    {
        return $query->where('recharge_api', 1);
    }


    public function getLastIpAttribute($ip)
    {
        return long2ip($ip);
    }

    public function setLastIpAttribute($ip)
    {
        $this->attributes['last_ip'] = ip2long($ip);
    }

    public function getRegIpAttribute($ip)
    {
        return long2ip($ip);
    }

    public function setRegIpAttribute($ip)
    {
        $this->attributes['reg_ip'] = ip2long($ip);
    }

    public function getIsProfiledAttribute()
    {
        if ($this->profile()->first()) {
            return true;
        }
        return false;
    }

    public function getIsRemittedAttribute()
    {
        if ($this->bank()->first()) {
            return true;
        }
        return false;
    }


    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        // TODO: Implement getJWTIdentifier() method.
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        // TODO: Implement getJWTCustomClaims() method.
        return [];
    }
}
